package servlet;

import eneity.News;
import service.UserManage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class newsDetailsServlet extends HttpServlet {
    UserManage userManage=new UserManage();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("newsID")!=null||req.getSession().getAttribute("newsID")!=null){
            int newsId= 0 ;
            if (req.getParameter("newsID")==null){
                newsId=(Integer) req.getSession().getAttribute("newsID");
            }else {
                newsId= Integer.parseInt(req.getParameter("newsID"));
            }
            req.getSession().setAttribute("newsID",newsId);
            List<News> commentslist = userManage.gettNewsComments((Integer) req.getSession().getAttribute("newsID"));
            List list = userManage.getNews(0, (Integer) req.getSession().getAttribute("newsID"));

            if ((req.getSession().getAttribute("username")!=null)){
                req.setAttribute("list",list);
                req.setAttribute("newsCommentsList",commentslist);
                News title=(News) list.get(0);
                req.setAttribute("newsTitle",title.getNtitle());
                req.setAttribute("username",(req.getSession().getAttribute("username")));
                req.getRequestDispatcher("newsDetail.jsp").forward(req,resp);
            }
            else {
                resp.sendRedirect("index.jsp");
            }
        }else {
            req.getRequestDispatcher("topicInfoServlet").forward(req,resp);
        }
    }
}
